﻿using System;
using System.Collections.Generic;

using System.Text;
using AhpilyServer;
using Island;

namespace Server
{
    class BattleHandler : IHandler
    {
        public void RegisterHandler(HandlerCenter center)
        {
            center.AddHandler(Protocol.ReqLocation,OnLocation);

            center.AddHandler(Protocol.ReqApplyFriend, OnApplyFriend);
            center.AddHandler(Protocol.ReqAgreeFriend, OnAgreeFriend);
        }

        private void OnLocation(ClientPeer token, byte[] data)
        {
            PBLocation pb = Util.Deserilize<PBLocation>(data);
            LocationCache.Instance.SetLoc(token.id,pb.latitude, pb.longitude);

            Console.WriteLine("Id:{0};N:{1};E:{2}", pb.id, pb.latitude, pb.longitude);
            ServerManager.SendOther(token, Protocol.ReqLocation, pb);
        }

        //A->B
        private void OnApplyFriend(ClientPeer token, byte[] data)
        {
            PBFriend pb = Util.Deserilize<PBFriend>(data);
            ClientPeer target = AccountCache.Instance.GetActor(pb.id);

            pb.id = token.id;
            pb.type = FriendType.APPLY;

            ServerManager.SendClient(target, Protocol.ReqApplyFriend, pb);
        }

        //B->A
        private void OnAgreeFriend(ClientPeer token, byte[] data)
        {
            PBFriend pb = Util.Deserilize<PBFriend>(data);

            //同意后，通知B添加成功
            if(pb.type == FriendType.AGREE)
            {
                ServerManager.SendClient(token, Protocol.RspAddFriend, pb);
            }

            //通知A，添加结果
            ClientPeer target = AccountCache.Instance.GetActor(pb.id);
            pb.id = token.id;
            ServerManager.SendClient(target, Protocol.ReqAgreeFriend, pb);

        }
    }
}
